﻿using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ModubsRTUTempactureCurve.Base
{

    public class LogProvider
    {
        public string LogPath {  get; set; }
        private readonly Object _lock = new Object();
        StringBuilder logstr = new StringBuilder();
        int count = 0;
        public LogProvider(string log_path)
        {
            LogPath = log_path;
        }

        public void WriteLog(string msg, bool isRecord = true, bool isError = false,[CallerLineNumber] int line = 0,[CallerMemberName] string methodName = "",[CallerFilePath] string filePath = "")
        {
            if (!isRecord) { return; }
            if(String.IsNullOrWhiteSpace(LogPath))
            {
                return;
            }

            WriteLog(new Log
            {
                Time = DateTime.Now,
                LogType = isError ? LogType.ERROR : LogType.LOG,
                Message = msg,
                ErrorFile = filePath,
                ErrorMethod = methodName, // 注意：原代码中是 ErroMethod，这里修正为 ErrorMethod 以匹配 Log 类定义
                ErrorLineNum = line,
            });
        }
        private void WriteLog(Log log)
        {
            lock (_lock)
            {
                count++;
                logstr.Append(log.ToString());
                if(count >= 100)
                {
                    SaveLog();
                    count = 0;
                    logstr.Clear();
                }
            }
        }

        public void Dispose()
        {
            SaveLog();
        }

        private void SaveLog()
        {
            string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
            string filePath = Path.Combine(this.LogPath, fileName);

            string delFile = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd") + ".log";
            string delfilePath = Path.Combine(this.LogPath, delFile);
            if (File.Exists(delfilePath))
            {
                File.Delete(delfilePath);
            }
            File.AppendAllText(filePath, logstr.ToString());
        }
    }

    
    public class Log
    {
        public DateTime Time { get; set; }
        public string? Message { get; set; }
        public LogType LogType { get; set; }

        public string? ErrorFile { get; set; }
        public string? ErrorMethod { get; set; }
        public int ErrorLineNum { get; set; }

        public override string ToString()
        {
            if(this.LogType == LogType.LOG)
            {
                return $"{Time.ToString("yyyy-MM-dd HH:mm:ss")} [{LogType.ToString()}] {Message}\r\n";
            }
            if (this.LogType == LogType.ERROR)
            {
                return $"{Time.ToString("yyyy-MM-dd HH:mm:ss")} [{LogType.ToString()}] {Message}\r\n" +
                    $"\t异常文件:{ErrorFile}\r\n" + $"\t异常位置:{ErrorMethod} [Line:{ErrorLineNum}]\r\n";
            }
            return base.ToString();
        }
    }

    public enum LogType
    {
        LOG,
        ERROR
    }
}
